<?php
/**
 * Dependency projects analyzer
 *
 * PHP version 5
 *
 * @category  W
 * @package   Services
 * @author    Olivier Hoareau <olivier@phppro.fr>
 * @copyright 2009 PHPPRO http://www.phppro.fr
 * @license   http://dev.phppro.fr/license PHPPRO
 * @version   SVN: $Id:$
 * @link      http://dev.phppro.fr/projects/wdependency
 */
require_once 'W/Dependency/Analyzer/Abstract.php';
/**
 * Dependency projects analyzer
 *
 * @category  W
 * @package   Services
 * @author    Olivier Hoareau <olivier@phppro.fr>
 * @copyright 2009 PHPPRO http://www.phppro.fr
 * @license   http://dev.phppro.fr/license PHPPRO
 * @version   SVN: $Id$
 * @link      http://dev.phppro.fr/projects/wdependency
 */
class W_Dependency_Analyzer_Projects extends W_Dependency_Analyzer_Abstract
{
    public function compute($data,$root)
    {
        $d = array();
        $dep = array();
        foreach($data as $file=>$info) {
            foreach(array('extends','implements','type','require_once','require','include','include_once','static') as $key) {
                if (false === isset($info[$key])) {
                    continue;
                }
                foreach($info[$key] as $item) {
                    if (false === isset($item['value'])) {
                        continue;
                    }
                    $value = $item['value'];
                    $dep[$file][] = $value;
                }
            }
        }
        foreach(array_keys($dep) as $file) {
            if (false === isset($data[$file]['class'][0])) {
                continue;
            }
            $dep[$data[$file]['class'][0]] = $dep[$file];
            unset($dep[$file]);
        }
        foreach($dep as $a=>$list) {
            if (false === isset($data[$a]['project'][0])) {
                continue;
            }
            $project = $data[$a]['project'][0];
            foreach($list as $b) {
                if (false === isset($data[$b]['project'][0])) {
                    continue;
                }
                if (false === isset($d[$project][$data[$b]['project'][0]])) {
                    $d[$project][$data[$b]['project'][0]] = 0;
                }
                $d[$project][$data[$b]['project'][0]]++;
            }
        }
        return $d;
    }
}